//
//  YZQWebViewController.m
//  A_project2.0
//
//  Created by apple on 16/3/15.
//  Copyright © 2016年 七平. All rights reserved.
//

#import "YZQWebViewController.h"
#import "QpDataBaseManager.h"

#import "QpfmdbModel.h"
@interface YZQWebViewController ()<UIWebViewDelegate>
{
    NSString *title;
}


@end

@implementation YZQWebViewController
- (void)viewWillAppear:(BOOL)animated
{
    // tabBar显示
    self.tabBarController.tabBar.hidden = YES;
}
// 页面即将消失 将tabBar隐藏
- (void)viewWillDisappear:(BOOL)animated
{
    // tabBar隐藏
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    UIWebView *wv = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height + 49)];
    [self.view addSubview:wv];
    [wv loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.str]]];
    
    

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"kongxin.png"] style:UIBarButtonItemStylePlain target:self action:@selector(right:)];
    // 获取webview的title
    wv.scalesPageToFit = YES;
    wv.delegate = self;
    NSURL *url = [[NSURL alloc]initWithString:self.str];
    NSURLRequest *request =  [[NSURLRequest alloc] initWithURL:url];
    [wv loadRequest:request];


    
    

}
#pragma mark - 获取webview的方法
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    NSLog(@"%@",title);
}
- (void)right:(UIBarButtonItem *)btn
{
    
    // 创建单例对象
    QpDataBaseManager *dbManger = [QpDataBaseManager shareInstance];
    [dbManger openDB];
    // 创建表
    [[QpDataBaseManager shareInstance] creatTable];
    
    // 查询表
    NSMutableArray *arr1 = [[QpDataBaseManager shareInstance] select];
    if (arr1) {
        
        for (QpfmdbModel *model in arr1) {
            if ([model.movieName isEqualToString:_str]) {
                //创建
                UIAlertController *al = [UIAlertController alertControllerWithTitle:@"提示" message:@"已经收藏" preferredStyle:UIAlertControllerStyleAlert];
                //显示
                [self presentViewController:al animated:YES completion:^{
                    // 一秒后执行diss方法
                    [self performSelector:@selector(diss:) withObject:self afterDelay:1];
                    
                }];
                NSLog(@"已经收藏 ");
                return;
            }
        }
        
    }
    // 创建数据库类
    QpfmdbModel *m = [[QpfmdbModel alloc] init];
    // 把需要放入数据库的数据防区数据库
    // 主文本
    m.movieName = _str;
    m.pubdate = title;
    // 调用数据库方法
    [[QpDataBaseManager shareInstance] insertCollection:m];

    //创建
    UIAlertController *al = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
    //显示
    [self presentViewController:al animated:YES completion:^{
        // 一秒后执行diss 方法
        [self performSelector:@selector(diss:) withObject:self afterDelay:1];
        
    }];
    
    
}
// 延时程序 将提示取消
- (void)diss:(id)a
{
    [self dismissViewControllerAnimated:YES completion:^{
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
